import React from "react";
import { Switch, Route, Redirect } from "react-router-dom";
export default function RouterView(props: any) {
	const { routes } = props;
	const componentList = routes.filter((item: any) => item.component);
	const redirectList = routes.filter((item: any) => item.to);
	// Switch 只能命中一个路由
	// Route 路由容器
	// Redirect 重定向组件
	return (
		<Switch>
			{componentList.map((item: any) => {
				return (
					<Route
						path={item.path}
						key={item.path}
						render={(history: any) => {
							return (
								<item.component
									{...history}
									routes={item.children || []}
								></item.component>
							);
						}}
					></Route>
				);
			})}
			{redirectList.map((item: any) => {
				return <Redirect key={item.to} {...item}></Redirect>;
			})}
		</Switch>
	);
}
